<template>
  <a-modal
    :title="`财务类型${model && model.id > 0 ? '编辑' : '新增'}`"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => {$emit('ok');getList()}"
    @cancel="() => {$emit('cancel')}"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" :labelCol="{ sm: { span: 4 } }" :wrapperCol="{ sm: { span: 20 } }">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item v-show="model && model.id > 0" label="ID">
          <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="名称">
          <a-input v-decorator="['name', { rules: [{ required: true, message: '名称不能为空' }] }]" placeholder="请输入财务名称"/>
        </a-form-item>
        <a-form-item label="类型">
          <a-select 
            v-decorator="['type', { rules: [{ required: true, message: '级别类型不能为空' }] }]" 
            placeholder="请选择类型"
            @change="handleChangeType"
          >
            <a-select-option v-for="it in typeList" :key="it.id" :value="Number(it.id)">{{ it.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="选项">
          <a-checkbox-group v-decorator="['data']" :options="needList" />
          <!-- <a-radio-group >
            <a-radio value="needEpiboly">外包公司</a-radio>
          </a-radio-group> -->
          <!-- <a-select v-decorator="['needEpiboly']">
            <a-select-option v-for="it in needList" :key="it.id" :value="Number(it.id)">{{ it.name }}</a-select-option>
          </a-select> -->
        </a-form-item>
        <a-form-item label="所属上级" v-show="!model && form.getFieldValue('type')">
          <a-select v-decorator="['topId']" :allowClear="true">
            <a-select-option v-for="it in topArr" :key="it.id" :value="Number(it.id)">{{ it.name }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { financeTypeListByNoAuth } from '@/api/noAuth'
// 表单字段
const fields = ['name', 'type', 'id','topId']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    typeList: {
      type: Array,
      default: () => [],
    },
    needList: {
      type: Array,
      default: () => [],
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      topList:[],
      topArr:[],
      data:[]
    }
  },
  created() {
    this.getList();
    

    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      let data = pick(this.model, fields)
      console.log(data)
      if(this.model){
        let need = ['needClient','needEpiboly','needFiliale','needProject','needStudio','needTb']
        data.data = [];
        need.forEach(v=>{
          console.log(this.model)
          if(this.model[v] == 1){
            data.data.push(v)
          }
        })
      }
      setTimeout(()=>{
        this.model && this.form.setFieldsValue(data)
      },0)
    })
  },
  methods: {
    getList(){
      financeTypeListByNoAuth({pageSize:100,pageNum:1})
      .then(res=>{
        console.log(res)
        if(res.data.data.length == 0) return 
        res.data.data.forEach((v,index)=>{
          if(v.topId !== 0)
          res.data.data.splice(index,1)
        })
        this.topList = res.data.data
      })
    },
    handleChangeType(val){
      let arr = []
      this.topList.forEach(v=>{
        if(val == v.type) arr.push(v)
      })
      this.topArr = arr
    }
  },
}
</script>
